//app.js
const api = require('./config/api.js')

App({

  onLaunch: function () {
    qq.getStorage({
      key: "xworldToken",
      success:res => {
        console.log(res.data)
        this.globalData.token = res.data
        //直接向服务器发token要信息
        qq.request({
          url: api.GetUserInfo,
          data: res,
          header: {
            'Content-Type': 'application/json',
            'Xworld-Token': this.globalData.token
          },
          method: 'POST',
          success: res => {
            console.log('res success qq.GetUserInfo', res)
            this.globalData.userInfo = res.data.userInfo
            this.globalData.xuserInfo = res.data.xuserInfo
            
            //向QQ要userInfo
            qq.getUserInfo({
              success:res => {
                //如果userInfo不一致，更新服务器的userInfo
                if (res.userInfo != this.globalData.userInfo) {
                  this.globalData.userInfo = res.userInfo
                  qq.request({
                    url: api.UploadUserInfo,
                    data: {"userInfo":res.userInfo},
                    header: {
                      'Content-Type': 'application/json',
                      'Xworld-Token': this.globalData.token
                    },
                    method: 'POST',
                    success: res => {
                      console.log('res success qq.UploadUserInfo', res)
                    },
                    fail: res => {
                      console.log('res fail', res)
                    }
                  })
                }
              },
              fail: res => {
                console.log('res fail', res)
              }
            })
          },
          fail: res => {
            console.log('res fail', res)
          }
        })
      },
      fail:res => {
        //向QQ发送请求得到code
        qq.login({
          success:res => {
            console.log("res qq.login get code", res)
            // 发送 res.code 到后台换取 openId和用户信息
            qq.request({
              url: api.AuthLoginByCode,
              data: {code: res.code},
              header: {
                'Content-Type': 'application/json',
                'Xworld-Token': ''
              },
              method: 'POST',
              success: res => {
                console.log('res success qq.login send code qq.req', res)
                this.globalData.token = res.data.token
                //把token存到缓存里
                qq.setStorage({
                  key: 'xworldToken',
                  data: res.data.token
                })
                //判断是不是新用户
                if (res.data.newUser) {
                  //新用户 向QQ要userInfo 更新服务器userInfo
                  qq.getUserInfo({
                    success:res => {
                      this.globalData.userInfo = res.userInfo
                      qq.request({
                        url: api.UploadUserInfo,
                        data: {"userInfo":res.userInfo},
                        header: {
                          'Content-Type': 'application/json',
                          'Xworld-Token': this.globalData.token
                        },
                        method: 'POST',
                        success: res => {
                          console.log('res success qq.UploadUserInfo', res)
                        },
                        fail: res => {
                          console.log('res fail qq.UploadUserInfo', res)
                        }
                      })
                    },
                    fail: res => {
                      console.log('res fail qq.getUserInfo', res)
                    }
                  })
                } else {
                  this.globalData.userInfo = res.data.userInfo
                  this.globalData.xuserInfo = res.data.xuserInfo
                  //向QQ要userInfo
                  qq.getUserInfo({
                    success:res => {
                      //如果userInfo不一致，更新服务器的userInfo
                      if (res.userInfo != this.globalData.userInfo) {
                        this.globalData.userInfo = res.userInfo
                        qq.request({
                          url: api.UploadUserInfo,
                          data: {"userInfo":res.userInfo},
                          header: {
                            'Content-Type': 'application/json',
                            'Xworld-Token': this.globalData.token
                          },
                          method: 'POST',
                          success: res => {
                            console.log('res success qq.UploadUserInfo', res)
                          },
                          fail: res => {
                            console.log('res fail qq.UploadUserInfo', res)
                          }
                        })
                      }
                    },
                    fail: res => {
                      console.log('res fail', res)
                    }
                  })
                }               
              }
            })
          },
          fail:res => {
            console.log('登录失败！',res)
          }
        })

      }
    })
    // 展示本地存储能力
    // 获取用户信息

    // 获取用户信息完成
    // 登录
    /*
    qq.login({
      success: res => {
        console.log("res qq.login get code", res)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        qq.request({
          url: api.AuthLoginByCode,
          data: {code: res.code},
          header: {
            'Content-Type': 'application/json',
            'Xworld-Token': ''
          },
          method: 'POST',
          success: res => {
            console.log('res success qq.login send code qq.req', res.data.token)
            this.globalData.token = res.data.token
            if(this.globalData.getselectdayCallback){
              this.globalData.getselectdayCallback(this.globalData.token)
            }
            qq.getSetting({
              success: res => {
                console.log(res)
                if (res.authSetting['scope.userInfo']) {
                  // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                  qq.getUserInfo({
                    success: res => {
                      this.globalData.userInfo = res.userInfo
                      let thatres = res
                      // 可以将 res 发送给后台解码出 unionId
                      qq.request({
                        url: api.SyncGetUserInfo,
                        data: res,
                        header: {
                          'Content-Type': 'application/json',
                          'Xworld-Token': this.globalData.token
                        },
                        method: 'POST',
                        success: res => {
                          console.log('res success qq.SyncGetUserInfo', res)
                          this.globalData.userInfo = res.data.data.userInfo
                          this.globalData.xuserInfo = res.data.data.xuserInfo
                          if (this.userInfoReadyCallback) {
                            this.userInfoReadyCallback(thatres)
                          }
                        },
                        fail: res => {
                          console.log('res fail', res)
                        }
                      })
                    }
                  })
                }
              }
            })
            qq.setStorageSync('token', res.data.token)
          },
          fail: res => {
            console.log('res fail', res)
          }
        })
      }
    })
    */
  },
  globalData: {
    userInfo: null,
    xuserInfo: null,
    otherCpInfo:[],
    iLikes:null,
    iLikesNum:0,
    likeMes:null,
    likeMeNum:0
  }
})
